home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLINC.PAK / WSKADDR.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  5KB  |  188 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.13  $
  6. //
  7. // Winsock for OWL subsystem.
  8. // Based on work by Paul Pedriana, 70541.3223@compuserve.com
  9. //----------------------------------------------------------------------------
  10. #if !defined(OWL_WSKADDR_H)
  11. #define OWL_WSKADDR_H
  12.  
  13. #if !defined(OWL_DEFS_H)
  14. # include <owl/defs.h>
  15. #endif
  16. #if !defined (_WINSOCKAPI_)
  17. # include <winsock.h>
  18. #endif
  19.  
  20. #if defined(BI_NAMESPACE)
  21. namespace OWL {
  22. #endif
  23.  
  24. // Generic definitions/compiler options (eg. alignment) preceeding the 
  25. // definition of classes
  26. #include <services/preclass.h>
  27.  
  28. //
  29. // class TSocketAddress
  30. // ~~~~~ ~~~~~~~~~~~~~~
  31. // TSocketAddress normally stores its data in network byte ordering, as
  32. // opposed to host byte ordering.
  33. //
  34. class _OWLCLASS TSocketAddress : public sockaddr {
  35.   public:
  36.     TSocketAddress();
  37.     TSocketAddress(const sockaddr& src);
  38.     TSocketAddress(const TSocketAddress& src);
  39.     TSocketAddress(ushort family, char* data, short dataLength);
  40.  
  41.     TSocketAddress& operator =(const sockaddr& src);
  42.     friend bool operator ==(const TSocketAddress& address1, const TSocketAddress& address2);
  43.  
  44.     ushort GetFamily() const;
  45.     void SetFamily(ushort family);
  46.     void SetAddress(ushort family, const char* data, short dataLength);
  47. };
  48.  
  49. //
  50. // class TINetSocketAddress
  51. // ~~~~~ ~~~~~~~~~~~~~~~~~~
  52. // Encapsulates the Internet socket address structure (sockaddr_in).
  53. //
  54. class _OWLCLASS TINetSocketAddress : public TSocketAddress {
  55.   public:
  56.     // Different classes of Internet addressing.  An address resolves to a
  57.     // 32-bit value. Each class has a different encoding scheme.
  58.     //
  59.     //
  60.     enum TINetClass {
  61.       ClassA,         // A class net addressing
  62.       ClassB,         // B class net addressing
  63.       ClassC,         // C class net addressing
  64.       ClassUnknown    // Unknown class net addressing
  65.     };
  66.  
  67.     TINetSocketAddress();
  68.     TINetSocketAddress(const sockaddr& src);
  69.     TINetSocketAddress(ushort newPort, ulong newAddress = INADDR_ANY,
  70.                        ushort newFamily = AF_INET);
  71.     TINetSocketAddress(ushort newPort, const char* newAddress,
  72.                        ushort newFamily = AF_INET);
  73.  
  74.     TINetSocketAddress& operator =(const sockaddr& src);
  75.     operator sockaddr_in() const;
  76.  
  77.     TINetClass GetClass() const;
  78.     ushort GetPort() const;
  79.     ulong GetNetworkAddress() const;
  80.     ulong GetNetwork() const;
  81.     ulong GetNetwork(ulong subnet) const;
  82.     ulong GetNode() const;
  83.     ulong GetNode(ulong subnet) const;
  84.     void SetAddress(ushort newFamily, ushort newPort, ulong newAddress);
  85.     void SetPort(ushort port);
  86.     void SetNetworkAddress(ulong address);
  87.     void SetNetworkAddress(const char* addressDottedDecimal);
  88.     void SetFiller();
  89.  
  90.     static ulong ConvertAddress(const char far* address);
  91.     static char far* ConvertAddress(ulong address);
  92.     static short IsAddressDottedDecimal(const char far* address);
  93.  
  94.     // These routines are rendered obsolete by newer versions
  95.     // that use const for the address argument.  Included for
  96.     // backwards compatibility.
  97.  
  98.     TINetSocketAddress(ushort newPort, char* newAddress,
  99.                        ushort newFamily = AF_INET);
  100.     void SetNetworkAddress(char* addressDottedDecimal);
  101.     static ulong ConvertAddress(char far* address);
  102.     static short IsAddressDottedDecimal(char far* address);
  103.  
  104. };
  105.  
  106. // Generic definitions/compiler options (eg. alignment) following the 
  107. // definition of classes
  108. #include <services/posclass.h>
  109.  
  110. #if defined(BI_NAMESPACE)
  111. } // namespace OWL
  112. #endif
  113.  
  114. //----------------------------------------------------------------------------
  115. // Inline implementations
  116. //
  117.  
  118. //
  119. // Return the family of addressing this address belongs to.
  120. //
  121. inline ushort
  122. TSocketAddress::GetFamily() const
  123. {
  124.   return sa_family;
  125. }
  126.  
  127. //
  128. // Set the family of addressing this address belongs to.
  129. //
  130. inline void
  131. TSocketAddress::SetFamily(ushort family)
  132. {
  133.   sa_family = family;
  134. }
  135.  
  136. //
  137. // Convert this address to the sockaddr_in structure.
  138. //
  139. inline
  140. TINetSocketAddress::operator sockaddr_in() const
  141. {
  142.   return *((sockaddr_in*)this);
  143. }
  144.  
  145. //
  146. // Return the network id masked with the subnet.
  147. //
  148. inline ulong
  149. TINetSocketAddress::GetNetwork(ulong subnet) const
  150. {
  151.   return GetNetworkAddress() & subnet;
  152. }
  153.  
  154. //
  155. // Return the node of this address.
  156. //
  157. inline ulong
  158. TINetSocketAddress::GetNode(ulong subnet) const
  159. {
  160.   return GetNetworkAddress() & ~subnet;
  161. }
  162.  
  163.  
  164. //
  165. //  Obsolete functions replaced by versions that make 
  166. //  use of const
  167. //
  168.  
  169. inline void
  170. TINetSocketAddress::SetNetworkAddress(char* addressDottedDecimal)
  171. {
  172.   SetNetworkAddress(CONST_CAST(const char*, addressDottedDecimal));
  173. }
  174.  
  175. inline static ulong
  176. TINetSocketAddress::ConvertAddress(char far* address)
  177. {
  178.   return ConvertAddress(CONST_CAST(const char far*, address));
  179. }
  180.  
  181. inline static short
  182. TINetSocketAddress::IsAddressDottedDecimal(char far* address)
  183. {
  184.   return IsAddressDottedDecimal(CONST_CAST(const char far*, address));
  185. }
  186.  
  187. #endif  // OWL_WSKADDR_H
  188.